//文件内容读取到数组中
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXLEN 1024

//读取文件filename的内容到dest数组中，最多读取maxlen字节
//成功就返回文件的字节数，否则-1
int read_file(const char *filename, char *dest, int maxlen) {
	FILE *file;
	int pos, temp, i;
	//打开文件
	file = fopen(filename, "r");
	if (NULL == file) {
		fprintf(stderr, "open %s error!\n", filename);
		return -1;
	}
	pos = 0;
	//循环读取文件内容
	for (i = 0; i < maxlen - 1; i++) {
		temp = fgetc(file);
		if (EOF == temp) {
			break;
		}
		dest[pos++] = temp;
	}
	//关闭文件
	fclose(file);
	//数组末尾加'\0'
	dest[pos] = 0;
	return pos;
}

int main(int argc, char *argv[]) {
	if (argc != 2) {
		fprintf(stderr, "Using: ./read <filename>\n");
		return -1;
	}
	char buffer[MAXLEN];
	int len = read_file(argv[1], buffer, MAXLEN);
	//打印文件内容
	printf("len: %d\ncontent: %s\n", len, buffer);
	return 0;
}
